<template>
  <div class="art-comments">
    <van-row class="art-head" type="flex" justify="space-between">
      <van-col><div>所有评论</div></van-col>
    </van-row>

    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="initComments"
    >
      <ArticleCommentItem
        v-for="comment in comments"
        :key="comment.id"
        :comment="comment"
      ></ArticleCommentItem>
    </van-list>

    <!-- 底部添加评论区域 - 1 -->
    <div class="add-cmt-box van-hairline--top" v-if="isShowBox1">
      <van-icon name="arrow-left" size="18" @click="$router.back()" />
      <div class="ipt-cmt-div" @click="showBox2">发表评论</div>
      <div class="icon-box">
        <van-badge :content="this.article.comment_count" :max="99">
          <van-icon
            name="comment-o"
            size="20"
            @click="$emit('click-comment')"
          />
        </van-badge>
      </div>
    </div>

    <!-- 底部添加评论区域 - 2 -->
    <div class="cmt-box van-hairline--top" v-else>
      <div class="left">
        <textarea
          placeholder="友善评论、理性发言、阳光心灵"
          @blur="hideBox2"
          ref="iptCmt"
          v-model.trim="cmt"
        ></textarea>
      </div>
      <van-button type="default" @click="pubCmt">发布</van-button>
    </div>
  </div>
</template>

<script>
import ArticleCommentItem from "@/components/Community/ArticleCommentItem";
import { commentListAPI, pubCommentAPI } from "@/api/articleApi";
import { mapState } from "vuex";
export default {
  components: {
    ArticleCommentItem,
  },
  data() {
    return {
      total: 0,
      page: 1,
      comments: [],
      loading: false,
      finished: false,
      isShowBox1: true,
      cmt: "",
    };
  },
  computed: {
    ...mapState("user", ["tokenInfo"]),
    ...mapState("article", ["article"]),
  },
  methods: {
    showBox2() {
      this.isShowBox1 = false;
      // 1. 将回调函数延迟到下次 DOM 更新完毕之后执行
      this.$nextTick(() => {
        // 2. 通过 ref 获取到 textarea 的引用
        this.$refs.iptCmt.focus();
      });
    },
    hideBox2(e) {
      if (e.relatedTarget && e.relatedTarget.className == "van-rate__item") {
        this.$nextTick(() => {
          // 2. 通过 ref 获取到 textarea 的引用
          this.$refs.iptCmt.focus();
        });
        return;
      }
      setTimeout(() => {
        // 隐藏第二个评论区域
        this.isShowBox1 = true;
        // 清空用户输入的评论内容
        this.cmt = "";
        this.rating = 0;
      }, 100);
    },
    async pubCmt() {
      const comment = {
        user_id: this.tokenInfo.user_id,
        content: this.cmt,
        article_id: this.$route.params.id,
      };
      const { data: res } = await pubCommentAPI(comment);
      if (res.message) {
        this.$notify({
          type: "warning",
          message: "评论失败",
          duration: 2000,
        });
        return;
      }
      this.comments.unshift(res);
      this.initArticle(this.article.id);
    },
    async initComments() {
      const { data: res } = await commentListAPI(
        this.$route.params.id,
        this.page
      );
      this.total = res.count;
      if (!res.next) {
        this.finished = true;
      }
      this.comments = [...this.comments, ...res.results];
      this.page++;
      this.loading = false;
    },
  },
  created() {
    this.initComments;
  },
};
</script>

<style lang="less" scoped>
.art-head {
  font-size: 16px;
  background-color: #f9f9f9;
  color: #666;
  padding: 10px 10px;
}

// 外层容器
.art-cmt-container-1 {
  padding-bottom: 46px;
}
.art-cmt-container-2 {
  padding-bottom: 80px;
}

// 发布评论的盒子 - 1
.add-cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  box-sizing: border-box;
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 46px;
  line-height: 46px;
  padding-left: 10px;
  .ipt-cmt-div {
    flex: 1;
    border: 1px solid #efefef;
    border-radius: 15px;
    height: 30px;
    font-size: 12px;
    line-height: 30px;
    padding-left: 15px;
    margin-left: 10px;
    background-color: #f8f8f8;
  }
  .icon-box {
    width: 20%;
    display: flex;
    justify-content: space-evenly;
    line-height: 0;
  }
}

.child {
  width: 20px;
  height: 20px;
  background: #f2f3f5;
}

// 发布评论的盒子 - 2
.cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 70px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  padding-left: 10px;
  box-sizing: border-box;
  background-color: white;
  .left {
    height: 70%;
    flex: 1;
    textarea {
      width: 90%;
      display: block;
      height: 70%;
      border: 1px solid #efefef;
      background-color: #f8f8f8;
      resize: none;
      border-radius: 6px;
      padding: 5px;
    }
  }
  .van-button {
    height: 50%;
    margin-right: 10px;
    border: none;
    background-color: #3a88f9;
    color: #fff;
  }
}
</style>
